<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <div th:include="Introduce/head"></div>
</head>
<style type="text/css">
    .talk_con {
        width: 600px;
        height: 500px;
        border: 1px solid #666;
        margin: 50px auto 0;
        background: #f9f9f9;
    }

    .talk_show {
        width: 580px;
        height: 420px;
        border: 1px solid #666;
        background: #fff;
        margin: 10px auto 0;
        overflow: auto;
    }

    .talk_input {
        width: 580px;
        margin: 10px auto 0;
    }


    .talk_word {
        width: 420px;
        height: 26px;
        padding: 0px;
        float: left;
        margin-left: 10px;
        outline: none;
        text-indent: 10px;
    }

    .talk_sub {
        width: 56px;
        height: 30px;
        float: left;
        margin-left: 10px;
    }

    .atalk {
        margin: 10px;
    }

    .atalk span {
        display: inline-block;
        background: #0181cc;
        border-radius: 10px;
        color: #fff;
        padding: 5px 10px;
    }

    .btalk {
        margin: 10px;
        text-align: right;
    }

    .btalk span {
        display: inline-block;
        background: #ef8201;
        border-radius: 10px;
        color: #fff;
        padding: 5px 10px;
    }
</style>


<body>
<div class="talk_con">
    <div class="talk_show" id="words">
        <div class="atalk"><span id="asay">A说：吃饭了吗？</span></div>
        <div class="btalk"><span id="bsay">B说：还没呢，你呢？</span></div>
    </div>
    <div class="talk_input">
        <input type="text" class="talk_word" id="talkwords">
        <input type="button" value="发送" class="talk_sub" id="talksub">
    </div>
</div>
</body>
</html>

<script type="text/javascript">

    //
    window.onload = function () {
        var Words = document.getElementById("words");
        var TalkWords = document.getElementById("talkwords");
        var TalkSub = document.getElementById("talksub");


        TalkSub.onclick = function () {
            //定义空字符串
            var str = "";
            if (TalkWords.value == "") {
                // 消息为空时弹窗
                alert("消息不能为空");
                return;
            }

            str = '<div class="btalk"><span>你 :' + TalkWords.value + '</span></div>';
            Words.innerHTML = Words.innerHTML + str;
            webSocket.send(TalkWords.value);
        }

    }
    var webSocket;
    var commWebSocket;
    if ("WebSocket" in window) {
        var username = '[[${username}]]';
        webSocket = new WebSocket("ws://j6aj94.natappfree.cc/websocket/" + username);

        //连通之后的回调事件
        webSocket.onopen = function () {
            //webSocket.send( document.getElementById('username').value+"已经上线了");
            console.log("已经连通了websocket");

        };

        //接收后台服务端的消息
        webSocket.onmessage = function (evt) {
            debugger
            var data=JSON.parse(evt.data)
            var Words = document.getElementById("words");
            str = '<div class="atalk"><span>'+ data.username+':' + data.message + '</span></div>';
            Words.innerHTML = Words.innerHTML + str;
        };

        //连接关闭的回调事件
        webSocket.onclose = function () {
            console.log("连接已关闭...");
        };
    } else {
        // 浏览器不支持 WebSocket
        alert("您的浏览器不支持 WebSocket!");
    }


</script>


